# encoding: gbk
import os
import sys
import MySQLdb
import sys

reload(sys)
sys.setdefaultencoding('gbk')


def getStatus(conn):
    query = " SHOW SLAVE STATUS "

    # print query
    cursor = conn.cursor()
    cursor.execute(query)
    result = cursor.fetchall()
    return result[0]


def resolve(conn):
    cursor = conn.cursor()
    query1 = "set global sql_slave_skip_counter=1"
    query2 = "START SLAVE"
    query3 = "SHOW SLAVE STATUS"

    cursor.execute(query1)
    cursor.execute(query2)
    cursor.execute(query3)
    conn.commit()


if __name__ == '__main__':
    conn = MySQLdb.connect(host="192.168.2.102", user="mars", passwd="mars123", db="", port=3306, charset="utf8")
    status = getStatus(conn)

    if status[32] is None:
        """resolve(conn)"""
        print 'wq not resolved'
    """else:
        print 'wq resolved"""

    conn = MySQLdb.connect(host="192.168.2.102", user="mars", passwd="mars123", db="", port=3307, charset="utf8")
    status = getStatus(conn)

    if status[32] is None:
        print 'yq pw not resolved'
    else:
        print 'yq pw resolved'

    conn = MySQLdb.connect(host="192.168.2.102", user="mars", passwd="mars123", db="", port=3308, charset="utf8")
    status = getStatus(conn)

    if status[32] is None:
        print 'ed not resolved'
    else:
        print 'ed resolved'

    conn = MySQLdb.connect(host="192.168.2.102", user="mars", passwd="mars123", db="", port=3309, charset="utf8")
    status = getStatus(conn)

    if status[32] is None:
        print 'eq not resolved'
    else:
        print 'eq resolved'

    conn = MySQLdb.connect(host="192.168.2.102", user="mars", passwd="mars123", db="", port=3310, charset="utf8")
    status = getStatus(conn)
    print "Master_Log_File: %s" % status[5]
    print "Read_Master_Log_Pos: %s" % status[6]
    print "Seconds_Behind_Master: %s" % status[-1]

    if status[32] is None:
        print 'sanq not resolved'
    else:
        print 'sanq resolved'

    conn = MySQLdb.connect(host="192.168.2.102", user="mars", passwd="mars123", db="", port=3311, charset="utf8")
    status = getStatus(conn)

    if status[32] is None:
        print 'bs not resolved'
    else:
        print 'bs resolved'

    """conn = MySQLdb.connect(host = "192.168.2.102", user ="mars",passwd = "mars123",db="", port=3312, charset="utf8")
    status = getStatus(conn)
    

    if status[32] is None:
        print '4q not resolved'
    else:
        print '4q resolved'"""

    conn = MySQLdb.connect(host="192.168.2.102", user="mars", passwd="mars123", db="", port=3313, charset="utf8")
    status = getStatus(conn)

    if status[32] is None:
        print 'js not resolved'
    else:
        print 'js resolved'

    conn = MySQLdb.connect(host="192.168.2.102", user="mars", passwd="mars123", db="", port=3314, charset="utf8")
    status = getStatus(conn)

    if status[32] is None:
        print 'cm not resolved'

    else:
        print 'cm resolved'

    conn = MySQLdb.connect(host="192.168.2.102", user="mars", passwd="mars123", db="", port=3316, charset="utf8")
    status = getStatus(conn)

    if status[32] is None:
        print 'yq jw not resolved'
    else:
        print 'yq jw resolved'

    conn = MySQLdb.connect(host="192.168.2.102", user="mars", passwd="mars123", db="", port=3317, charset="utf8")
    status = getStatus(conn)

    if status[32] is None:
        print 'djw not resolved'
    else:
        print 'djw resolved'

    conn = MySQLdb.connect(host="192.168.2.102", user="mars", passwd="mars123", db="", port=3318, charset="utf8")
    status = getStatus(conn)

    if status[32] is None:
        print 'xx not resolved'
    else:
        print 'xx resolved'

    conn = MySQLdb.connect(host="192.168.2.102", user="mars", passwd="mars123", db="", port=3319, charset="utf8")
    status = getStatus(conn)

    if status[32] is None:
        print '4q jw not resolved'
    else:
        print '4q jw resolved'

    conn = MySQLdb.connect(host="192.168.2.102", user="mars", passwd="mars123", db="", port=3321, charset="utf8")
    status = getStatus(conn)

    if status[32] is None:
        print '6q not resolved'
    else:
        print '6q resolved'

    conn = MySQLdb.connect(host="192.168.2.102", user="mars", passwd="mars123", db="", port=3322, charset="utf8")
    status = getStatus(conn)

    if status[32] is None:
        print 'leo not resolved'
    else:
        print 'leo resolved'

    print 'check resolved'
